/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gac.gacmod.visual;

import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import org.gac.gacmod.model.CompositeStep;
import org.gac.gacmod.model.Step;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.anchor.AnchorShape;
import org.netbeans.api.visual.router.RouterFactory;
import org.netbeans.api.visual.widget.ConnectionWidget;

/**
 *
 * @author Tomek
 */
public class SplitStepWidget extends CompositeStepWidget
{

    private int _childCount = 0;
    private ConnectionWidget _firstConnection;
    private DropAreaWidget _firstDropArea;

    public SplitStepWidget(BoxScene scene, Step modelStep)
    {
        super(scene, modelStep);
        _clientArea.setBorder(BorderFactory.createEmptyBorder(40, 25, 40, 25));

        _clientArea.setLayout(Layouts.HorizontalLayout);

        _firstDropArea = new DropAreaWidget(scene, true,
                (CompositeStep) _modelStep, _childCount++);

        _clientArea.addChild(_firstDropArea);

        ConnectionWidget edge = new ConnectionWidget(scene);
        edge.setSourceAnchor(AnchorFactory.createDirectionalAnchor(
                _headerVFlow, AnchorFactory.DirectionalAnchorKind.VERTICAL));
        edge.setTargetAnchor(AnchorFactory.createDirectionalAnchor(
                _bottomWidget, AnchorFactory.DirectionalAnchorKind.VERTICAL));
        edge.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        //edge.setMaximumSize( new Dimension( 2,2));
        //edge.setRouter (RouterFactory.createOrthogonalSearchRouter( new LayerWidget(scene)) );
        edge.setRouter(RouterFactory.createOrthogonalSearchRouter());
        _firstConnection = edge;
        _scene.getConnectionLayer().addChild(edge);

    }

    @Override
    public void addInnerStep(StepWidgetBase step)
    {
        step._parent = this;
        if (_firstDropArea != null)
        {
            _clientArea.removeChild(_firstDropArea);
            _scene.removeDropAreaWidget(_firstDropArea);

            _clientArea.addChild(new DropAreaWidget(_scene, false,
                    (CompositeStep) _modelStep, _childCount - 1));
            _firstDropArea.setVisible(false);
            _firstDropArea = null;
        }
        _clientArea.addChild(new InnerStepWrapperWidget(_scene, step, true,
                (CompositeStep) _modelStep, _childCount++));

        _firstConnection.setVisible(false);

        ConnectionWidget edge1 = new ConnectionWidget(_scene);
        edge1.setSourceAnchor(AnchorFactory.createFreeRectangularAnchor(
                _headerVFlow, true));
        edge1.setTargetAnchor(AnchorFactory.createFreeRectangularAnchor(
                step, true));
        edge1.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        //edge.setMaximumSize( new Dimension( 2,2));
        edge1.setRouter(SimpleOrthogonalRouter.upperLinksRouter);

        ConnectionWidget edge2 = new ConnectionWidget(_scene);
        edge2.setSourceAnchor(AnchorFactory.createFreeRectangularAnchor(
                step, true));
        edge2.setTargetAnchor(AnchorFactory.createFreeRectangularAnchor(
                _bottomWidget, true));
        edge2.setSourceAnchorShape(new StraightArrow(12, true, true, false, 11.0));
        //edge.setMaximumSize( new Dimension( 2,2));
        edge2.setRouter(SimpleOrthogonalRouter.lowerLinksRouter);

        _scene.getConnectionLayer().addChild(edge1);
        _scene.getConnectionLayer().addChild(edge2);
    }

    @Override
    public void removeInnerStep(StepWidgetBase step)
    {
        ((CompositeStep) _modelStep).RemoveStep(step._modelStep);
    }
}
